package com.cxyzy.broadcastdemo

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        button.setOnClickListener {
            val intent = Intent("com.cxyzy.broadcastdemo.bomb")
            intent.setPackage(packageName)
            sendOrderedBroadcast(intent, null)
        }
    }
}

//第一个广播接收器
class FirstBroadcastReceiver : BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent) {
        Toast.makeText(context, "1号站点收到通知，有💣", Toast.LENGTH_SHORT).show()

    }
}

//第二个广播接收器
class SecondBroadcastReceiver : BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent) {
        Toast.makeText(context, "2号站点收到通知，有💣", Toast.LENGTH_SHORT).show()
        //阻断广播继续传播
//        abortBroadcast()
    }
}